package com.wbl.train.business;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.ArrayList;

/**
 * Author: 王博纶
 * Date: 2024/2/1 13:24
 * Description: business的启动类
 */
@ComponentScan("com.wbl.train")
@SpringBootApplication
// 开启openFeign
@EnableFeignClients("com.wbl.train.common.feign")
// 启动springboot内置的cache缓存
@EnableCaching
public class BusinessTrainApplication {
    private static final Logger Log = LoggerFactory.getLogger(BusinessTrainApplication.class);

    public static void main(String[] args) {
        SpringApplication businessApp = new SpringApplication(BusinessTrainApplication.class);
        ConfigurableEnvironment environment = businessApp.run(args).getEnvironment();
        Log.info("启动成功！！");
        Log.info("网关地址：\thttp://127.0.0.1:{}",environment.getProperty("server.port"));

//        // 限流规则
//        initFlowRules();
//        Log.info("sentinel限流规则被调用");
    }

    /**
     * sentinel配置规则资源的方法
     */
    private static void initFlowRules(){
        ArrayList<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("confirmOrderDo");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(1);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}